package com.example.mylibrarybase.yumengmeng07.ui;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.mylibrarybase.R;
import com.example.mylibrarybase.bar.initAction_Bar;
import com.example.mylibrarybase.base.baseui.BaseActivity;
import com.example.mylibrarybase.net.netWork;
import com.example.mylibrarybase.utils.LogUtils;
import com.example.mylibrarybase.utils.UtilsToast;
import com.example.mylibrarybase.yumengmeng07.bean.BiddingDetailbean_07;
import com.example.mylibrarybase.yumengmeng07.utils.linesRadioGroup.Arith;
import com.example.mylibrarybase.yumengmeng07.utils.linesRadioGroup.LineRadioGroup;
import com.example.mylibrarybase.yumengmeng07.view.TableListView_07;
import com.lzy.okgo.cache.CacheMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity_list_07 extends BaseActivity {

    private TableListView_07 mActivity_list_tablelistview;
    private BiddingDetailbean_07 mData;
    private initAction_Bar mBase_action_bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public int getlayouXML() {
        return R.layout.activity_main_activity_list_07;
    }

    @Override
    public void initView() {
       mActivity_list_tablelistview = (TableListView_07) findViewById(R.id.activity_list_tablelistview);
        mBase_action_bar = (initAction_Bar) findViewById(R.id.base_action_bar);
        mBase_action_bar
                .setMode(initAction_Bar.AUTO_ONCLICK_MODE)
                .setCallBack(new initAction_Bar.Action_bar_call_back() {
                    @Override
                    public void getAction_barView_backbutton(TextView textView) {
                        textView.setText("返回");
                    }

                    @Override
                    public void getAction_barView_backbutton_icon(ImageView imageView) {

                    }

                    @Override
                    public void getAction_barView_title(TextView textView) {
                        textView.setText("动态生成表格");
                    }

                    @Override
                    public void getAction_barView_right_icon(ImageView imageView) {

                    }
                });

        initData();
    }

    @Override
    public void initData() {

        netWork.goPost(this, netWork.testlist, null, "2", CacheMode.NO_CACHE, new netWork.CallBack() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                mData = jsonObject.getObject("data", BiddingDetailbean_07.class);
                LogUtils.i("当前获取数据为"+mData);
                setlistData(mData);
            }

            @Override
            public void onError(String error) {

            }
        });



    }

    private void setlistData(BiddingDetailbean_07 data) {
        //左面
        ArrayList<LineRadioGroup.leftname> innerClasses0 = new ArrayList<>();
        //上面
        ArrayList<LineRadioGroup.leftname> innerClassess1 = new ArrayList<>();
        //右面结算
        ArrayList<LineRadioGroup.leftname> innerClassess2 = new ArrayList<>();
        //右面个数
        ArrayList<LineRadioGroup.leftname> innerClassess3 = new ArrayList<>();
        //加载右下角的
        List<LineRadioGroup.FeedbackOptionItem> optionsList = new ArrayList<>();
        for (int i = 0; i < data.accident.size(); i++) {
            List<BiddingDetailbean_07.DataBean> accident = data.accident;
            BiddingDetailbean_07.DataBean dataBean = accident.get(i);
            LogUtils.i("给每一个横条的 数据 是啥" + dataBean);
            //给上面和左面的集合添加数据
            if (i <= 5) {
                innerClassess1.add(new LineRadioGroup.leftname(i + "", dataBean.type+"", dataBean.rows+"", dataBean.num));
            }
            if (i == 0) {
                innerClasses0.add(new LineRadioGroup.leftname(i + "", dataBean.name + "", dataBean.rows + "", dataBean.num));
                innerClassess3.add(new LineRadioGroup.leftname(i + "", dataBean.name+"", dataBean.rows+"", dataBean.num));
                //  innerClassess2.add(new LineRadioGroup.leftname(i + "", dataBean.name, dataBean.rows, dataBean.num));
            }
            if (i >= 3 && i % 4 == 0) {
                LogUtils.i("横着的i是" + i);
                innerClasses0.add(new LineRadioGroup.leftname(i + "", dataBean.name+"", dataBean.rows+"", dataBean.num));
                //  innerClassess2.add(new LineRadioGroup.leftname(i + "", dataBean.name, dataBean.rows, dataBean.num));
                innerClassess3.add(new LineRadioGroup.leftname(i + "", dataBean.name+"", dataBean.rows+"", dataBean.num));
            }
            optionsList.add(new LineRadioGroup.FeedbackOptionItem("" + i, dataBean.price+"", dataBean.name+"", dataBean.type+"", dataBean.rows+"", dataBean.num,dataBean.span));

        }
        //  0 左面  1 上面 2 行结算 3个数
        mActivity_list_tablelistview.initData(0,innerClasses0);
        mActivity_list_tablelistview.initData(3,innerClassess3);
        mActivity_list_tablelistview.initData(1,innerClassess1);
        innerClassess2.addAll(innerClasses0);
        mActivity_list_tablelistview.initData(2,innerClassess2);

        mActivity_list_tablelistview.initchebox(optionsList, new LineRadioGroup.CallBackItemNum() {
            @Override
            public void callback(int pos, String title, int linespos, String num, String span) {
                LogUtils.i("我点的是"+"pos"+"\t"+pos+"\t"+"title"+"\t"+title+"\t"+"linespos"+"\t"+linespos+"\t"+"num"+"\t"+num+"\t"+"span"+"\t"+span);
                ArrayList<TextView> arrayList =
                        LineRadioGroup.getArrayList1();
                TextView textView = arrayList.get(linespos);
                textView.setText(Double.parseDouble(title) + "*" + num + "=" + Arith.mul(Double.parseDouble(title),Double.parseDouble(num)));
                ArrayList<LineRadioGroup.FeedbackOptionItem> feedbackOptionItems = toletTableSumcenl();
                double sum = getSum(feedbackOptionItems);
                LogUtils.i("点击了按钮的集合总价钱" + sum + "内容" + feedbackOptionItems.toString());
                UtilsToast.showToast(MainActivity_list_07.this, "总价钱"+sum);
            }
        });



    }
    /**
     *
     * 统计结果 多行单选 带取消
     */
    public static ArrayList<LineRadioGroup.FeedbackOptionItem> toletTableSumcenl() {
        ArrayList<LineRadioGroup.FeedbackOptionItem> intsx = new ArrayList<>();
        ArrayList<CheckBox[]> arrayList =
                LineRadioGroup.getArrayList0box();
        //LogUtils.i("生成多行的集合数据"+arrayList);

        for (int i = 0; i < arrayList.size(); i++) {

            CheckBox[] checkBoxes = arrayList.get(i);
            boolean tag = false;
            for (int j = 0; j < checkBoxes.length; j++) {
                CheckBox checkBox = checkBoxes[j];
                //必须 别问为什么 必须加判断
                if (checkBox.isChecked()&&!tag) {
                    //有被点击的
                    tag = true;
                    LineRadioGroup.FeedbackOptionItem tag1 = (LineRadioGroup.FeedbackOptionItem) checkBox.getTag();
                    intsx.add(tag1);
                }

                //判断这一行 如果没有被点击的 就弹出来东西
//                if (j == radioGroup.getChildCount() - 1 && !tag) {
//                    LogUtils.i("有没有勾选上的" + "目前集合里面已经勾选的有" + intsx);
//                    UtilsToast.showToast(c, "还有没勾选上的条目");
//                    return null;
//                }
            }
            //如果没有被点击的
            if (!tag){

            }
        }

        getSum(intsx);

        return intsx;
    }
    public static double getSum(ArrayList<LineRadioGroup.FeedbackOptionItem> intsx) {
        double sum;
        sum = 0;
        if (intsx.size()>0&&intsx!=null){
            for (int i = 0; i < intsx.size(); i++) {
                LineRadioGroup.FeedbackOptionItem feedbackOptionItem = intsx.get(i);
                double add = Arith.mul(Double.parseDouble(feedbackOptionItem.num), Double.parseDouble(feedbackOptionItem.title));
                // float i1 = Integer.parseInt(feedbackOptionItem.num) * Float.parseFloat(feedbackOptionItem.title);
                //sum += add;
                sum = Arith.add(sum, add);

            }

        }

        return sum;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.i("报价详情页面关闭 这里实用工具类 创建的集合 清空 方便下次 在进来的时候使用LineRadioGroup.setmoveList();");
        LineRadioGroup.setmoveList();
    }
}
